package com.corner.ui.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.App;
import com.corner.app.ConsTants;
import com.corner.model.cornerservice.domain.User;
import com.corner.presenter.imp.LoginPresenter;
import com.corner.ui.activity.LoginAndRegisterActivity;
import com.corner.ui.base.BaseActivity;
import com.corner.ui.base.BaseFragment;
import com.corner.ui.view.ILoginView;
import com.corner.utils.SoftInputUtil;
import com.corner.utils.ToastUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;

public class LoginFragment extends BaseFragment<LoginPresenter> implements OnClickListener, ILoginView {
    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.btn_register_login_actionbar_back)
    private Button btnBack;
    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.btn_register_login_actionbar_right)
    private Button btnRegister;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_frag_login_notice)
    private TextView tvNotice;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_phone_number_zone)
    private EditText etTimeZone;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_phone_number_input)
    private EditText etPhoneInput;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.et_password_input)
    private EditText etPasswordInput;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_login_finish)
    private TextView tvFinish;

    @CustomFont(ConsTants.TYPE_FACE)
    @FindView(R.id.tv_forget_password)
    private TextView tvForgetPassword;

    @Override
    protected LoginPresenter createP() {
        return new LoginPresenter();
    }

    @Override
    protected void setOnclick() {
        tvFinish.setOnClickListener(this);
        tvForgetPassword.setOnClickListener(this);
        btnBack.setVisibility(View.GONE);
        btnRegister.setOnClickListener(this);
        btnRegister.setVisibility(View.VISIBLE);
    }

    @Override
    protected View initView(LayoutInflater inflater) {
        return View.inflate(activity, R.layout.frag_login, null);
    }

    @Override
    public void onClick(View v) {
        SoftInputUtil.hideSoftInput(activity, view);
        switch (v.getId()) {
            case R.id.tv_login_finish:
                finishLogin();
                break;
            case R.id.tv_forget_password:
                activity.changeFragment(
                        ((LoginAndRegisterActivity) activity).getForgetPasswordFragment(),
                        this, true);
                break;
            case R.id.btn_register_login_actionbar_right:
                btnRegister:
                activity.changeFragment(
                        ((LoginAndRegisterActivity) activity).getGetSmsCodeFragment(),
                        this, true);
                break;
        }
    }

    /**
     * 完成登录
     */
    private void finishLogin() {
        activity.excuteTaskWhenNetOk(new BaseActivity.Task() {

            @Override
            public void onExcute() {
                if (checkPhone(etPhoneInput.getText())) {
                    if (checkPassword(etPasswordInput.getText())) {
                        presenter.login(etPhoneInput.getText().toString(), etPasswordInput
                                .getText().toString());
                    } else {
                        ToastUtil.showCustomToastInRelease(activity,
                                R.string.login_password_null_notice);
                    }
                } else {
                    ToastUtil.showCustomToastInRelease(activity,
                            R.string.login_error_phone_notice);
                }
            }
        });
    }

    /**
     * 校验电话
     *
     * @param s
     * @return
     */
    private boolean checkPhone(CharSequence s) {
        String regex = "1[358][0-9]{9}";
        return s.toString().matches(regex);
    }

    /**
     * 校验电话
     *
     * @param s
     * @return
     */
    private boolean checkPassword(CharSequence s) {
        return s.length() != 0;
    }

    @Override
    public void loginSucess(User user) {
        ToastUtil.showCustomToastInDevelop(activity,
                R.string.login_sucess);
        ((LoginAndRegisterActivity) activity).goHome();
    }

    @Override
    public void loginError(String errorMessage) {
        ToastUtil.showCustomToastInRelease(App.application, errorMessage);
    }
}
